// pages/classification/classificationGoods/index.js
const common = require('../../../../../public.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cid: '',
    pageNum: 1,
    goodsList: [],
    noMoreData: false,
    isLoadingBusy: true,
    sort:10    //页数量
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (opt) {
    this.data.cid = opt.cid
    const that = this
    getApp().getLocation(function (data) {
        that.data.lon = data.longitude
        that.data.lat = data.latitude
        that.getGoodsList(true)
      },
      function (err) {
        that.setData({
          isLoadingBusy: false
        })
      })
  },
  getGoodsList(refresh = false) {
    // 当前非刷新状态，正在加载数据或已经没有数据，不需要再次请求数据
    if (!refresh && (this.data.isLoadingBusy || this.data.noMoreData)) {
      return
    }
    if (!refresh) {
      this.setData({
        isLoadingBusy: true
      })
    } else {
      this.setData({
        refreshing: true,
        noMoreData: false
      })
    }
    const that = this
    const pageNum = refresh ? 1 : that.data.pageNum + 1

    const tempJson = {
      regionId:common.regionId,
      cid: that.data.cid,
      pageNum,
      sort:that.data.sort,
      xpoint: that.data.lon,
      ypoint: that.data.lat
    }
    common.post('/wlplatfrom/item/cate/listForXcx', tempJson, function (res) {
      if (res.status === 200) {
        // if(that.data.cid == '870'){// 演出
        //   console.log('演出屏蔽---')
        //   res.data = []
        // }
        if (res.data == '') {
          that.setData({
            noMoreData: true
          })
          return
        }
        that.setData({
          pageNum,
          goodsList: refresh ? res.data : [...that.data.goodsList, ...res.data]
        })
      }
    }, function () {
      that.setData({
        isLoadingBusy: false
      })
    })
  },
  toItemDetail(e) {
    common.gotoGoodsPage(e.currentTarget.dataset.item)
  },
  onReachBottom() {
    this.getGoodsList(false)
  },
})